<?php
namespace App\Models\Chain\PointCash;




use App\Constants\CreditLogConstant;
use App\Helpers\Utils;
use App\Models\Factory\InviteFactory;
use App\Models\Chain\AbstractHandler;
use App\Models\Factory\UserCreditFactory;
use App\Models\Orm\AwUserIntegralExchange;

/**
 * 插入用户积分兑换表
 * Class CreateCreditLogAction
 * @package App\Models\Chain\Identity
 */

class CreateIntegralExchangeAction extends AbstractHandler
{
    private $params = array();

    protected $error = array('error' => '插入积分兑换流水表', 'code' => 7015);


    public function __construct($params)
    {
        $this->params = $params;
    }

    public function handleRequest()
    {
        if ($this->CreateIntegralExchange($this->params) == true) {
            $this->setSuccessor(new CreateUserCreditLogAction($this->params));
            return $this->getSuccessor()->handleRequest();
        } else {
            return $this->error;
        }
    }


    public function CreateIntegralExchange($params)
    {

        // 生成提现记录
        $insert_data = [
            'user_id' => $params['userId'],
            'integral' => $params['credit'],
            'money' => $params['money'],
            'status' =>AwUserIntegralExchange::STATUS_INIT,
            'ip' => strval(ip2long(Utils::ipAddress())),
        ];

        $params['order_id'] = AwUserIntegralExchange::insertGetId($insert_data);

        $this ->params = $params;
        return $params['order_id'] ? true : false;
    }


}